package com.csy.common.exception;

import com.csy.admin.util.Result;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 类名:全局异常处理(controller层)
 * Created by CSY on 2018/9/13.
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = RRException.class)
    public Result handleRRException(RRException e){
        Result result = new Result();
        logger.error(e.getMessage());
        result.put("code",e.getCode());
        result.put("msg",e.getMsg());
        return result;
    }

    /**
     * 处理shiro授权异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = AuthorizationException.class)
    public Result handleAuthorizationException(AuthorizationException e){
        logger.error(e.getMessage(),e);
        return Result.error("没有授权，请联系管理员");
    }
    /**
     * 全局异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(Exception ex){
        logger.error(ex.getMessage(),ex);
        return Result.error();
    }
}
